/**
* 
	* DPM LOOK LIKE PAST 
* see <a href="{@docRoot}/Documentation/report.html"> for more information</a>
*  Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN
* @author: Urunuela Richard
* @version 3.2
*/
package storm.DPMS;

import java.util.ArrayList;
import java.util.Iterator;

import storm.Processors.LEATProcessor;
import storm.Processors.Processor;
/**
 * 
 * @author richard
 *
 */
public class DPM_PAST extends DPM {
	
	private double top;
	private double bottom;
	/**
	 * 
	 */
	public void init(){
		top = this.getOwnFieldIntValue("top") ;
		bottom = this.getOwnFieldIntValue("bottom") ;
		ArrayList cpus = this.Kernel.getTasksListeManager().getALLProcessors();
		Iterator I = cpus.iterator();
		while (I.hasNext()){
			Processor P= (Processor) I.next();
			P.setOwnFieldIntValue("load", 0);
		}
	}
	public void onTick(){
		ArrayList cpus = this.Kernel.getTasksListeManager().getALLProcessors();
		Iterator I = cpus.iterator();
		while (I.hasNext()){
			Processor P= (Processor) I.next();
			if (P instanceof LEATProcessor){
				//Change frequency in time 
				LEATProcessor LP = (LEATProcessor) P;
			//if (LP.isRunning() ) P.setOwnFieldIntValue("load",P.getOwnFieldIntValue("load")+1);
				//double load = P.getOwnFieldIntValue("load")/((this.Kernel.getTime()+1)*1.0);
				double load = (((double)P.getLoad())/(double)(this.Kernel.getTime()+1)) * 100;
			//	System.out.println(this.top + " -- " +this.bottom + " -- "  + " --  " + load + " -- " + LP.getOwnFieldIntValue("load"));
				//System.out.println(load); 
				if (load > top){
					int f = LP.getFreq();
					LP.setFreq(f-1);
					//System.out.println(" - " + LP.getFreq() + " -  " + f ); 
				}
				if (load < bottom){
					int f = LP.getFreq();
					LP.setFreq(f+1);
					//System.out.println(" + " + LP.getFreq()+ " -  " + f ); 
				}
				
			}
			
		
		}
	}

}
